package com.europe.business.europebusiness.ui.activity.base;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.RegisteredActivity;

public abstract class ActionBarActivity extends BaseActivity {
    private static final String TAG = "ActionBarActivity";
    private Toolbar toolbar;
    private LinearLayout content;
    private TextView title;
    private TextView actionRight;
    private EditText search;
    private TextView searchBtn;
    private LinearLayout searchLin;
    private LinearLayout statusBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_actionbar;
    }

    @Override
    protected void initView() {
        initToolbar();
        statusBar = findViewById(R.id.status_bar);
        content = findViewById(R.id.content);
        actionRight = findViewById(R.id.action_bar_right);
        search = findViewById(R.id.search);
        searchBtn = findViewById(R.id.search_btn);
        searchLin = findViewById(R.id.search_lin);

        int layout = setMainContent();
        View view = LayoutInflater.from(this).inflate(layout, content, false);
        content.addView(view);
//        search.addTextChangedListener(getTextWatcher());
    }

    protected void initToolbar() {
        toolbar = findViewById(R.id.toolbar);
        title = findViewById(R.id.title);
        setSupportActionBar(toolbar);
        setActionbarBackground(getResources().getColor(R.color.white));
        toolbar.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setToolbarNavigationOnClicked();
            }
        });

    }

    protected TextWatcher getTextWatcher() {
        return null;
    }

    /**
     * ActionBar左上角的返回箭头的点击事件，子类需要的话，要复写此方法
     */
    protected void setToolbarNavigationOnClicked() {
        onBackPressed();
    }

    /**
     * 子类设置返回键的图标
     *
     * @param drawable
     */
    protected void setToolbarNavigationIcon(int drawable) {
        toolbar.setNavigationIcon(drawable);
    }


    protected void setActionRight(boolean isVisible, String string, View.OnClickListener listener) {
        if (isVisible) {
            actionRight.setVisibility(View.VISIBLE);
            actionRight.setText(string);
            actionRight.setOnClickListener(listener);
        } else {
            actionRight.setVisibility(View.GONE);
        }
    }

    protected String getSearchKey() {
        return search.getText().toString();
    }

    /**
     * ActionBar右上角的按钮点击事件，子类需要的话，要复写此方法
     **/
    protected void regClicked() {
    }

    /**
     * 隐藏actionbar（toolbar）
     */
    protected void setToolbarVisible(int visible) {
        toolbar.setVisibility(visible);
    }

    /**
     * 设置全屏幕(透明状态栏)
     */
    protected void setFullScreen() {
        if (Build.VERSION.SDK_INT >= 21) {
//            View decorView = getWindow().getDecorView();
//            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//            decorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.RED);
            getWindow().setStatusBarColor(Color.RED);
        }
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.hide();
    }

    protected void setWindowStatusBarColor(int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 子类设置标题
     *
     * @param titleStr
     */
    protected void setTitle(String titleStr) {
        title.setVisibility(View.VISIBLE);
        searchLin.setVisibility(View.GONE);
        title.setText(titleStr);
    }

    /**
     * 搜索框是否可见
     *
     * @param isVisible
     */
    protected void setSearchVisible(boolean isVisible, View.OnClickListener listener) {
        if (isVisible) {
            searchLin.setVisibility(View.VISIBLE);
            title.setVisibility(View.GONE);
            actionRight.setVisibility(View.GONE);
            searchBtn.setOnClickListener(listener);
        } else {
            searchLin.setVisibility(View.GONE);
            title.setVisibility(View.VISIBLE);
//            actionRight.setVisibility(View.VISIBLE);
        }


    }

    protected void setActionbarBackground(int color) {
        toolbar.setBackgroundColor(color);
    }

    protected abstract int setMainContent();
}
